﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

public class SceneManager
{
    private static SceneManager instance = null;

    public static SceneManager Instance
    {
        get
        {
            if (instance == null)
            {
                lock (typeof(SceneManager))
                {
                    if (instance == null)
                    {
                        instance = new SceneManager();
                    }
                }
            }
            return instance;
        }
    }

    private EditorApplication.CallbackFunction updateCallback = null;
    private bool initDone = false;

    private SceneManager()
    {
        Debug.Log("SceneManager SceneManager() called");

        updateCallback = new EditorApplication.CallbackFunction(EditorUpdate);
        if (EditorApplication.update == null)
        {
            EditorApplication.update += updateCallback;
        }
        else if (!EditorApplication.update.Equals(updateCallback))
        {
            EditorApplication.update += updateCallback;
        }

        initDone = true;
    }

    public bool InitDone
    {
        get
        {
            return initDone;
        }
    }

    public void Init()
    {
    }

    private bool duplicateCheck = false;

    public void EditorUpdate()
    {
        MaxstAR.ICameraDevice cameraDevice = (MaxstAR.ICameraDevice)UnityEngine.Object.FindObjectOfType(
                typeof(MaxstAR.ICameraDevice));

        if (cameraDevice != null)
        {
            cameraDevice.Init();
        }

        MaxstAR.ITrackableBehaviour[] trackables = (MaxstAR.ITrackableBehaviour[])UnityEngine.Object.FindObjectsOfType(
                typeof(MaxstAR.ITrackableBehaviour));

        if (duplicateCheck == false)
        {
            CheckForDuplicates(trackables);
            duplicateCheck = true;
        }
    }

    public void SceneUpdated()
    {
        duplicateCheck = false;
    }

    private void CheckForDuplicates(MaxstAR.ITrackableBehaviour[] trackables)
    {
        for (int i = 0; i < trackables.Length; ++i)
        {
            string nameA = trackables[i].TrackableName;

            if (nameA == MaxstAR.MaxstARUtils.GlobalVars.DefaultNoneMessage)
            {
                continue;
            }

            for (int j = i + 1; j < trackables.Length; ++j)
            {
                string nameB = trackables[j].TrackableName;

                // Ignore default names...
                if (nameB == MaxstAR.MaxstARUtils.GlobalVars.DefaultNoneMessage)
                {
                    continue;
                }

                if (trackables[i].TrackableId == trackables[j].TrackableId)
                {
                    Debug.LogError("Duplicate Trackables detected: \"" + nameA);
                }
            }
        }
    }
}

